package com.paishen.peiniwan.module.date;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.paishen.common.dialog.LoadingDialog;
import com.paishen.common.util.BaseUtil;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.BusEvent;
import com.paishen.peiniwan.C;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.paishen.peiniwan.frame.http.HttpResp;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.peiniwan.frame.http.entity.Book;
import com.paishen.peiniwan.frame.http.entity.DateInfo;
import com.paishen.peiniwan.frame.http.entity.User;
import com.paishen.peiniwan.frame.widget.StarView;
import com.paishen.common.widget.TitleLayout;
import com.paishen.peiniwan.module.profile.ProfileInfoActivity;
import com.squareup.okhttp.Request;

import de.greenrobot.event.EventBus;

public class DateCommentActivity extends BaseActivity {

    private Book book;
    private DateInfo dateInfo;

    private User other;

    private ImageView imgAvatar;
    private TextView txNickname;
    ;
    private StarView viewStar;

    private EditText edComment;
    private TextView txLimitedCount;
    private boolean isEditRemark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dateInfo = (DateInfo) getIntent().getSerializableExtra(DateInfoActivity.EXTRA_DATE_INFO);
        if (dateInfo == null || dateInfo.book == null || (book = dateInfo.book.get(0)) == null) {
            BaseUtil.showToast(R.string.error_data);
            finish();
            return;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_comment);

        if (App.i().getUser().getId() == dateInfo.user.id) {
            other = book.user;
        } else {
            other = dateInfo.user;
        }

        if (other == null || other.id <= 0 ) {
            BaseUtil.showToast(R.string.error_user);
            return;
        }

        findViewById(R.id.lay_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hideSoftInputFromWindowAlways(DateCommentActivity.this);
            }
        });

        final ScrollView viewScroll = (ScrollView) findViewById(R.id.view_scroll);
        viewScroll.setVisibility(View.VISIBLE);
        viewScroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isEditRemark) {
                    isEditRemark = false;
                    BaseUtil.hideSoftInputFromWindowAlways(DateCommentActivity.this);
                }
                return false;
            }
        });

        TitleLayout layTitle = (TitleLayout) findViewById(R.id.lay_title);
        layTitle.pushContent(makeBackView(), getString(R.string.date_comment), null, false);

        imgAvatar = (ImageView) findViewById(R.id.img_avatar);
        imgAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ProfileInfoActivity.class);
                intent.putExtra(ProfileInfoActivity.EXTRA_SHOWN_USER_ID, other.id);
                v.getContext().startActivity(intent);
            }
        });

        txNickname = (TextView) findViewById(R.id.tx_nickname);
        viewStar = (StarView) findViewById(R.id.view_star);
        viewStar.setEditable(true);


        Glide.with(this).load(other.head_img_url).bitmapTransform(App.i().getCropCircleTransformation())
                .into(imgAvatar);

        txNickname.setText(other.nickname);

        edComment = (EditText) findViewById(R.id.ed_comment);
        edComment.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // first click
                    viewScroll.smoothScrollTo(0, viewScroll.getHeight());
                    isEditRemark = true;
                }
            }
        });
        edComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewScroll.smoothScrollTo(0, viewScroll.getHeight());
                isEditRemark = true;
            }
        });

        txLimitedCount = (TextView) findViewById(R.id.tx_limited_count);
        edComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                final int maxLength = 200;
                txLimitedCount.setText(String.valueOf(maxLength - edComment.getText().length()));
            }
        });

        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewStar.getLightCount() == 0) {
                    BaseUtil.showToast(R.string.date_comment_comment_tips_1);
                    return;
                }
                String commentText = edComment.getText().toString();
                if (TextUtils.isEmpty(commentText) || commentText.length() < 15) {
                    BaseUtil.showToast(R.string.date_comment_comment_tips);
                    return;
                }


                requestCommentAdd();
            }
        });


    }

    private void requestCommentAdd() {
        float score = viewStar.getLightCount();

        String content = edComment.getText().toString();
        final LoadingDialog dlgLoading = new LoadingDialog.Builder(this).create();
        dlgLoading.show();
        PnwHttpManager.Comment.add(TAG, dateInfo.id, score, content, new PnwOkCallbackAdapter<HttpResp>() {
            @Override
            public void onResponse(HttpResp response, boolean fresh) {
                super.onResponse(response, fresh);
                dlgLoading.dismiss();
                if (response.errcode == C.http.RESULT_SUCCESS) {

                    finish();
                } else if (fresh && response.errcode != C.http.RESULT_LOGIN_EXPIRE &&
                        response.errcode != C.http.RESULT_LOGIN_OFFLINE) {
                    BaseUtil.showToast(response.errmsg);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                dlgLoading.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 更新我的约会状态
        EventBus.getDefault().post(new BusEvent(BusEvent.ACTION_REFRESH_MAIN_PROFILE_DATE));
    }

}
